# -*- coding: utf-8 -*-
'''
import os
from subprocess import Popen,PIPE

p = Popen('netstat -lntp'.split(),stdout=PIPE,stderr=PIPE)
p.wait()
netstat = p.stdout.read()
if b'8022' not in netstat:
    os.system('dropbear')
'''
import os

# 安装dropbbear
os.system('apt update&&apt install dropbear -y')

# 配置dropbear密钥和authorized_keys
os.chdir('/data/data/com.termux/files/home')
if not os.path.exists('.ssh'):os.mkdir('.ssh')
command = 'dropbearkey -t rsa -f /sdcard/qpython/id_rsa | grep "^ssh-rsa " >>.ssh/authorized_keys'
os.system(command)

# 配置自启动dropbear服务
with open('.dropbear.py','w') as f:
    f.write(__doc__)
with open('.bashrc', 'a+') as f:
    f.write('\npython .dropbear.py\n')
os.system('dropbear')

print('dropbear服务配置完成')
